package com.mirzabros.chexmo;

import org.apache.http.cookie.Cookie;
import org.apache.http.util.EncodingUtils;

import com.mirzabros.chexmo.contant.Chexmo_Urls;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.RenderPriority;
import android.widget.Button;


public class Chexmo_Webview extends Activity {
	    private WebView webView;
	    
	    private String pass_user;
	    private String pass_pass;
	    private String pass_code;
	    private String pass_code2;
	    
	    private ProgressDialog progdialod;
	    
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        
	        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	        setProgressBarIndeterminateVisibility(true); 
	        
	        setContentView(R.layout.chexmo_webview);
	        
	        /*if (savedInstanceState != null){
	            ((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState);
	    	}*/
	        
	        Button btnlogouyt = (Button)findViewById(R.id.back);
	        btnlogouyt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onBackPressed();
					
				}
			});
	        
	        Bundle extras = getIntent().getExtras();
			if (extras != null) {
				pass_user = extras.getString("user");
				pass_pass = extras.getString("pass");
				pass_code = extras.getString("code");
				pass_code2 = extras.getString("code2");
				
			}
	        
	        webView = (WebView) findViewById(R.id.webView);
	        
	        //webView.getWindowToken();
	        
	        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
	        imm.hideSoftInputFromWindow(webView.getWindowToken(), 0);
	        
	        
	        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
	        webView.setPadding(0, 0, 0, 0);
	        webView.setHorizontalScrollBarEnabled(false);
	        webView.setInitialScale(50);
	        
	        WebSettings webSettings = webView.getSettings();
	        
	        //PRIYAN - For webview Acceleration 
	        webSettings.setRenderPriority(RenderPriority.HIGH);
	        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

	        //if (Build.VERSION.SDK_INT >= 11){
	        //	webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
	        //}
	        
	      //PRIYAN - For webview Acceleration 
	        
	        webSettings.setJavaScriptEnabled(true);
	        webSettings.setLoadWithOverviewMode(true);
	        webSettings.setUseWideViewPort(true);
	        
	        webSettings.setAllowUniversalAccessFromFileURLs(true);
	        webSettings.setAllowFileAccessFromFileURLs(true);
	        
	        //webView.loadUrl("http://mb.demoplease.com/service-api/storeadmin/123456");
	        //webView.loadUrl("http://www.google.com");
	        
	        //webView.loadUrl("http://mb.demoplease.com/store-customer-login");
	        
	        //http://mb.demoplease.com/xmlrpc-login-redirect/code
	        
	        //String postData = "user="+pass_user+"&pass="+pass_pass;
	        //webView.postUrl("http://mb.demoplease.com/xmlrpc-login-request",EncodingUtils.getBytes(postData, "BASE64"));
	        
	        webView.loadUrl(Chexmo_Urls.WebView_CustomerLogin_Forward+pass_code+"/"+pass_code2);//
	        
	        //webView.setWebViewClient(new WebViewClient());
	        webView.setWebViewClient(new MyWebViewClient(Chexmo_Urls.WebView_CustomerLogin_Forward+pass_code+"/"+pass_code2));
	
	        //PRIYAN :) Try Inbuilt Code For Webview with input type Number
	        //String summary = "<html><body><h1>Sample form page</h1><form id='sampleform' method='post' action='' ><p>Name: <input type='number' name='Name' /></p><p>Email: <input type='text' name='Email' /></p></form></body></html>";
	        //webView.loadData(summary, "text/html", null);
	        //PRIYAN :) Try
	    }
	    
	    private class MyWebViewClient extends WebViewClient {
	    	
	    	private String NumberURL;
	    	
	    	public MyWebViewClient(String numberurl) {
	    		Log.e("Constructor", "Constructor");
	    		NumberURL = numberurl;
			}
	    	
	        /*@Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            if (Uri.parse(url).getHost().equals("devarea.chexmo.com")) {
	            	Log.e("False", "False");
	                return false;
	            }
	            
	            Log.e("NumberURL", NumberURL);
	            Log.e("url", url);
	            
	            if(NumberURL.equals(url)){
	            	Log.e("In IF", "In IF");
	            	view.setOnTouchListener(new View.OnTouchListener() {
						@Override
						public boolean onTouch(View v, MotionEvent event) {
							
							return false;
						}     
	                    });
	            	
	            	 Activity activity = (Activity) view.getContext();
	                 InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
	                 inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
	            }
	            
	            
	            
	            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
	            startActivity(intent);
	            return true;
	        }*/
	    	
	    	@Override
	        public boolean shouldOverrideUrlLoading(WebView webview, String url){
	                webview.loadUrl(url);
	                setProgressBarIndeterminateVisibility(true);
	                                return true;

	            }
	    	
	    	@Override
	    	public void onPageStarted(WebView view, String url, Bitmap favicon) {
	    		progdialod = ProgressDialog.show(Chexmo_Webview.this, "", "Please Wait",true,false);
	    	}
	        
	        @Override
	        public void onPageFinished(WebView view, String url) {
	        	
	        	Log.e("NumberURL", NumberURL);
	            Log.e("url", url);
	            progdialod.dismiss();
	            setProgressBarIndeterminateVisibility(false);
	            
	          if (url.equals(Chexmo_Urls.WebView_BackURL)) {
	        	  Log.e("In IF", "In IF");
	            	finish();
	            	
	            	//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	            	
	            	
	            	
	            	 //Activity activity = (Activity) view.getContext();
	                 InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
	                 inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
	            
	          }
	        }
	    }	
	 
	    @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	        if(event.getAction() == KeyEvent.ACTION_DOWN){
	            switch(keyCode)
	            {
	            case KeyEvent.KEYCODE_BACK:
	                if(webView.canGoBack()){
	                	webView.goBack();
	                }else{
	                    finish();
	                }
	                return true;
	            }

	        }
	        return super.onKeyDown(keyCode, event);
	    }
	        
	    
	protected void onSaveInstanceState(Bundle outState) {
	   //webView.saveState(outState);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig){        
	    super.onConfigurationChanged(newConfig);
	}
}

